package com.itheima.work;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @auther Macrydy
 * @date 2020/7/9 17:56
 *
 * 使用AtomicInteger原子类代替基本类型变量。
 */
public class Test1 {
    static AtomicInteger ai = new AtomicInteger(1000);

    public static void main(String[] args) {


        new Thread(new Runnable() {
            @Override
            public void run() {
                method();
            }

        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                method();
            }

        }).start();

    }

    private synchronized static void method() {
        while (true){
            if(ai.get()>0){
                ai.decrementAndGet();
                System.out.println(Thread.currentThread().getName()+"======>"+ai.get());
            }else {
                break;
            }
        }

    }
}
